import { AdManager } from "../ads/AdManager";
import { AudioManager1 } from "../manager/AudioManager1";
import { UIManager } from "../manager/UIManager";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM, GAME_TYPE_ENUM, RELEASE_TYPE_ENUM } from "../base/EnumIndex";
import { EventManager } from "../manager/EventManager";
import { ResManager1 } from "../manager/ResManager1";
import { GameManager } from "../manager/GameManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class startUI extends cc.Component {
    @property({ type: cc.Button, displayName: "添加按钮" })
    addBtn: cc.Button = null;
    // @property({ type: cc.Button, displayName: "测试按钮" })
    // testShareBtn: cc.Button = null;
    @property({ type: cc.Button, displayName: "分享按钮" })
    shareBtn: cc.Button = null;
    @property({ type: cc.Button, displayName: "排行榜按钮" })
    rankBtn: cc.Button = null;
    @property({ type: cc.Button, displayName: "礼包按钮" })
    giftBtn: cc.Button = null;
    @property({ type: cc.Button, displayName: "常玩按钮" })
    changwanBtn: cc.Button = null;
    @property({ type: cc.Node, displayName: "收藏按钮" })
    favouriteBtn: cc.Node = null;

    @property({ type: cc.Node, displayName: "logo节点" })
    logo: cc.Node = null;

    @property({ type: cc.Node, displayName: "用户协议" })
    agree: cc.Node = null;
    @property({ type: cc.Node, displayName: "隐私政策" })
    privacy: cc.Node = null;
    @property({ type: cc.Button, displayName: "入口奖励按钮" })
    entryBtn: cc.Button = null;

    onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.HIDE_GIFT_BTN, this.hideGiftBtn, this);
        EventManager.ins.on(EVENT_NAME_ENUM.HIDE_CHANGWAN_BTN, this.hideChangwanBtn, this);
        EventManager.ins.on(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN, this.hideAddBtn, this);
    }
    start() {
        AdManager.showBanner();
        AudioManager1.playBG("startBgm", 1);

        this.updateBtn();
        AdManager.send_JuLiang();
        AdManager.showAgreeUI();
        AdManager.reportHomeView();
        GameManager.ins.startUIPop();
        this.updateLogo();

        // ResManager.ins.preloadRes('prefab/ui/levelUI', cc.Prefab, BUNDLE_TYPE_ENUM.HOME);
        // ResManager1.ins.preloadRes('prefab/ui/LoadGameUI', cc.Prefab, BUNDLE_TYPE_ENUM.HOME);

        if (AdManager.releaseType === RELEASE_TYPE_ENUM.ks) {
            this.node.getChildByName("ks_info").active = true;
        } else {
            this.node.getChildByName("ks_info").active = false;
        }

    }
    updateBtn() {
        this.addBtn.node.active = false;
        // this.giftBtn.node.active = true;
        // AdManager.isShowAddShortcutBtn((active: boolean) => {
        //     this.addBtn.node.active = active;
        // });
        this.shareBtn.node.active = AdManager.isShowShareBtn();
        this.rankBtn.node.active = AdManager.isShowRankBtn();
        AdManager.isShowGiftBtn((active: boolean) => {
            console.log("是否开启入口按钮", active);
            // this.giftBtn.node.active = active;
            this.entryBtn.node.active = active;
        });
        AdManager.isShowChangWanBtn((active: boolean) => {
            console.log("是否开启常玩按钮2", active);
            this.changwanBtn.node.active = active;
        })
        this.agree.active = AdManager.isShowPrivacy();
        this.privacy.active = AdManager.isShowPrivacy();
        this.favouriteBtn.active = AdManager.isShowFavouriteBtn();
    }
    updateLogo() {
        this.logo.children.forEach(async i => {
            let url = `texture/logo/${AdManager.getLogoFolderName()}/${i.name}`
            let spf = await ResManager1.ins.loadRes(url, cc.SpriteFrame, BUNDLE_TYPE_ENUM.HOME_EXTRA)
            i.getComponent(cc.Sprite).spriteFrame = spf;
        })
    }

    hideGiftBtn() {
        // this.giftBtn.node.active = false;
        this.entryBtn.node.active = false;
    }
    hideChangwanBtn() {
        this.changwanBtn.node.active = false;
    }
    hideAddBtn() {
        this.addBtn.node.active = false;
    }

    onDestroy() {
        EventManager.ins.off(EVENT_NAME_ENUM.HIDE_GIFT_BTN, this.hideGiftBtn, this);
        EventManager.ins.off(EVENT_NAME_ENUM.HIDE_CHANGWAN_BTN, this.hideChangwanBtn, this);
        EventManager.ins.off(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN, this.hideAddBtn, this);
        AdManager.hideBanner();
        // AdManager.hideMoreGame();
    }

    clickBtnStart() {
        AudioManager1.playClick();
        UIManager.ins.close();

        UIManager.ins.showUI("LevelUI", BUNDLE_TYPE_ENUM.HOME);
    }
    clickBtnSetting() {
        AudioManager1.playClick();
        UIManager.ins.showPop("settingUI", BUNDLE_TYPE_ENUM.HOME);
    }
    clickBtnAddShortCut() {
        AudioManager1.playClick();
        // AdManager.addTable(() => {
        //     this.addBtn.node.active = false;
        // });
        // UIManager.ins.showPop('addShortcutUI', BUNDLE_TYPE_ENUM.HOME);
        AdManager.addTable(() => EventManager.ins.emit(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN));
    }

    clickBtnShare() {
        AudioManager1.playClick();
        AdManager.shareFriends();
    }

    clickBtnRank() {
        AudioManager1.playClick();
        AdManager.getRankList();
    }
    clickBtnGift() {
        AudioManager1.playClick();
        // if (AdManager.releaseType == RELEASE_TYPE_ENUM.wechat || AdManager.releaseType === RELEASE_TYPE_ENUM.ks) {
        //     UIManager.ins.showPop('signUI', BUNDLE_TYPE_ENUM.HOME);
        //     return;
        // }
        // if (AdManager.fuFangFinished()) {
        //     UIManager.ins.showPop('signUI', BUNDLE_TYPE_ENUM.HOME);
        // } else {
        //     UIManager.ins.showPop('douyinPropUI', BUNDLE_TYPE_ENUM.HOME);
        // }
        UIManager.ins.showPop('signUI', BUNDLE_TYPE_ENUM.HOME);
    }

    clickBtnEntry(){
        AudioManager1.playClick();
        UIManager.ins.showPop('douyinPropUI', BUNDLE_TYPE_ENUM.HOME);
    }

    clickBtnChangWan() {
        AdManager.addCommonUse(() => {
            this.hideChangwanBtn();
        })
    }

    clickBtnFavourite() {
        AudioManager1.playClick();
        AdManager.favoriteGuide((active: boolean) => {
            // this.favouriteBtn.active = active;
        });
    }


    clickBtnUserDoc() {
        AudioManager1.playClick();
        UIManager.ins.showPop('agreeUI', BUNDLE_TYPE_ENUM.HOME);
    }
    clickBtnPrivacy() {
        AudioManager1.playClick();
        UIManager.ins.showPop('privacyUI', BUNDLE_TYPE_ENUM.HOME);
    }

}
